﻿#include "MaterialFloatNodeViewModel.h"

#include "MaterialFloatNodeModel.h"

#include "Framework/Editor.h"

#include "MaterialGraph/Pin/MaterialGraphPinViewModel.h"
#include "MaterialGraph/Pin/MaterialGraphPinModel.h"

#include "MaterialGraph/MaterialGraphViewModel.h"

#include "MaterialGraph/UndoRedo/MaterialEditOperation.h"

void RMaterialFloatNodeViewModel::BindModel(TSharedObjectPtr<RMaterialGraphNodeModel> InModel, TSharedObjectPtr<RMaterialGraphViewModel> InGraph)
{
    Super::BindModel(InModel,InGraph);
    FloatModel=InModel;

    FloatModel->OnIsParameterChanged.Bind([this](){
        if(FloatModel->IsParameter())
        {
            OutputViewModels[0]->SetLineIndex(2);
        }
        else
        {
            OutputViewModels[0]->SetLineIndex(1);
        }

        OnNodeLineChanged.ExecuteIfBound();
    });
    if(FloatModel->IsParameter())
    {
        OutputViewModels[0]->SetLineIndex(0);
    }
    else
    {
        OutputViewModels[0]->SetLineIndex(1);
    }
}

int64_t RMaterialFloatNodeViewModel::GetLineNum() const
{
    if(FloatModel->IsParameter())
    {
        //参数节点标题占用两行
        return 4;
    }
    return 3;
}

bool RMaterialFloatNodeViewModel::IsParameter() const
{
    return FloatModel->IsParameter();
}

void RMaterialFloatNodeViewModel::ToggleParameter()
{
    auto ChangePropertyOperation=NewObject<RMaterialFunctionOperation>();

    bool OldValue=FloatModel->IsParameter();
    bool NewValue=!OldValue;
    ChangePropertyOperation->Objects.Add(FloatModel); //确保对象不会被销毁
    RMaterialFloatNodeModelWeakPtr WeakFloatModel=FloatModel;
    ChangePropertyOperation->RedoFunction.Bind([WeakFloatModel,NewValue](TSharedObjectPtr<RMaterialGraphModel>){
        WeakFloatModel->SetIsParameter(NewValue);
    });
    ChangePropertyOperation->UndoFunction.Bind([WeakFloatModel,OldValue](TSharedObjectPtr<RMaterialGraphModel>){
        WeakFloatModel->SetIsParameter(OldValue);
    });
   

    GraphViewModel->RedoOperation(ChangePropertyOperation);
    GraphViewModel->AddOperation(ChangePropertyOperation);


}

CString RMaterialFloatNodeViewModel::GetParameterName() const
{
    return FloatModel->GetParameterName();
}

void RMaterialFloatNodeViewModel::SetParameterName(const CString &InName)
{
    auto ChangePropertyOperation=NewObject<RMaterialChangeObjectPropertyOperation>();
    ChangePropertyOperation->
        SetObjectAndOffset(
            FloatModel
            ,offsetof(RMaterialFloatNodeModel,ParameterName)
        );
    ChangePropertyOperation->OldValue=FloatModel->ParameterName;
    ChangePropertyOperation->NewValue=InName;
    ChangePropertyOperation->bCanCombine=true;

    GraphViewModel->RedoOperation(ChangePropertyOperation);
    GraphViewModel->AddOperation(ChangePropertyOperation);
}

float RMaterialFloatNodeViewModel::GetValue() const
{
    return FloatModel->Value;
}

void RMaterialFloatNodeViewModel::SetValue(float InValue)
{
    auto ChangePropertyOperation=NewObject<RMaterialChangeObjectPropertyOperation>();
    ChangePropertyOperation->
        SetObjectAndOffset(
            FloatModel
            ,offsetof(RMaterialFloatNodeModel,Value)
        );
    ChangePropertyOperation->OldValue=FloatModel->Value;
    ChangePropertyOperation->NewValue=InValue;
    ChangePropertyOperation->bCanCombine=true;

    GraphViewModel->RedoOperation(ChangePropertyOperation);
    GraphViewModel->AddOperation(ChangePropertyOperation);
}
